<?php

namespace app\admin\model;

use app\api\model\Qrcode;
use app\common\model\User;
use extend\Poster as PosterExtend;
use think\Model;

require_once ROOT_PATH.'extend/Poster.php';


class Bg extends Model
{

    

    

    // 表名
    protected $name = 'bg';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }






    //海报
    public function poster($bg_id,$qrcode_param,$app_type)
    {
        $page = 'pages/index/index';

        $qrcode_info = $this->getQrcode($app_type, $page, $qrcode_param);

        if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;

        $member_info = User::get($qrcode_param[ 'suid' ]);
        if (empty($member_info))throw new \Exception('未获取到会员信息');

        $bg_info = Bg::get($bg_id);
        if (empty($bg_info))throw new \Exception('未获取到背景图信息');

        try {

            $poster = new PosterExtend(602, 1150);
            $option = [
                [
                    'action' => 'setBackground', // 背景色
                    'data' => [239, 246, 252]
                ],
                [
                    'action' => 'imageCopy', // 写入背景图片
                    'data' => [
                        ROOT_PATH.'public'.$bg_info['image'],
                        0,
                        0,
                        602,
                        916,
                    ]
                ],
                [
                    'action' => 'imageCopy', // 写入二维码
                    'data' => [
                        $qrcode_info[ 'data' ][ 'path' ],
                        405,
                        965,
                        155,
                        155
                    ]
                ],
                [
                    'action' => 'imageCopy', // 写入头像
                    'data' => [
                        ROOT_PATH.'public'.$member_info[ 'avatar' ],
                        20,
                        950,
                        70,
                        70,
                        60
                    ]
                ],
                [
                    'action' => 'imageText',
                    'data' => [
                        $member_info['nickname'],
                        25,
                        [ 32, 44, 80 ],
                        110,
                        1000,
                        440,

                    ]
                ],
                [
                    'action' => 'imageText',
                    'data' => [
                        '邀请您加入哇哇来购',
                        20,
                        [ 32, 44, 80 ],
                        20,
                        1070,
                        440,

                    ]
                ],
                [
                    'action' => 'imageText',
                    'data' => [
                        '长按识别二维码,立即加入',
                        18,
                        [ 156, 161, 177 ],
                        20,
                        1130,
                        440,
                        1
                    ]
                ],
            ];

            $option_res = $poster->create($option);
            if (is_array($option_res)) return $option_res;

            $res = $option_res->jpeg('upload/poster/share',  $bg_id.'_'.$qrcode_param[ 'suid' ] . '_' . $app_type);

            if ($res['code']==0){
                return $res['data']['path'];
            } else {
                throw new \Exception($res['message']);
            }
            //return $res;

        }  catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取二维码
     * @param unknown $app_type 请求类型
     * @param unknown $page uniapp页面路径
     * @param unknown $qrcode_param 二维码携带参数
     * @param string $promotion_type 活动类型 null为无活动
     */
    private function getQrcode($app_type, $page, $qrcode_param)
    {
        $data = [
            'app_type' => $app_type,
            'type' => 'get',
            'data' => $qrcode_param,
            'page' => $page,
            'qrcode_path' => 'upload/qrcode/share',
            'qrcode_name' => 'share' . '_' . $qrcode_param[ 'suid' ],
        ];
        $qrcode_model = new Qrcode();
        $res = $qrcode_model->createQrcode($data);
        return $res;
    }



}
